<?php
namespace App\Services\PingAnPay;

use App\Services\PingAnPay\lib\pingan;

/*
 * 平安银行支付demo
 * UnionAPI_Open开通卡接口调用
 *
 */
class UnionApiOpen{

     /**
     * notify_url和return_url的设置要是全网可以访问的，并且商户的服务器不限制平安访问
     */
    /*private $notify_url = 'https://域名/路径/notify.php';
    private $return_url = 'https://域名/路径/return.php';*/
    private $notify_url = '/notify.php';
    private $return_url = '/return.php';
    //如果手机端出现证书过期可以使用http://testebank.orangebank.com.cn/khpayment/UnionAPI_Open.do
    // private $gateway = 'http://testebank.sdb.com.cn/khpayment/UnionAPI_Open.do';
    private $gateway = 'https://testebank.sdb.com.cn/khpayment/UnionAPI_Open.do';

    /**
     * 开始执行
     * @param  [type] $customerId [商户会员号]
     * @return [type]             [description]
     */
    public function run($customerId){

        // $masterId = $_GET['masterId'];
        $masterId = config('pinganpay.masterId');
        //$customerId = $_GET['customerId'];
        $customerId = $customerId;

        //组装订单数据
        $data = array(
                'masterId' => $masterId,
                'customerId'=> $customerId,
                'orderId' =>  $masterId.date('Ymd'). rand(10000000, 99999999),
                'dateTime' => date('YmdHis'),
        );

        $data = $this->_getData($data);

        // $merchantCertFile = '../merchant/2000311146.pfx';
        $merchantCertFile = config('pinganpay.merchant_cert_file');

        //获取orig和sign
        list($orig, $sign) = $this->_getOrigAndSing($merchantCertFile, $data);

        $parameter = array(
                'orig' => $orig,
                'sign' => $sign,
                /*'returnurl' =>$return_url,
                'NOTIFYURL' =>$notify_url,*/
                'returnurl' => config('pinganpay.open_return_url'),
                'NOTIFYURL' => config('pinganpay.open_notify_url'),
        );

        $this->showHtml($parameter);
    }

    /**
     * 进行数据提交
     * @param array $parameter
     */
    private function showHtml($parameter) {
        $html = '<html><head><meta charset="UTF-8" /></head><body>';
        $html .= '<form method="post" name="P_FORM" action="' . $this->gateway . '">';
        foreach ($parameter as $key => $val) {
            $html .= "<input type='hidden' name='$key' value='$val' />";
        }
        $html .= '</form><script type="text/javascript">document.P_FORM.submit();</script>';
        $html .= '</body></html>';
        echo $html;
    }

    /**返回报文
     *
     * array(13) {
     * ["errorCode"]=> string(0) ""
     * ["errorMsg"]=> string(0) ""
     * ["masterId"]=> string(10) "unionpay01"
     * ["plantBankId"]=> string(5) "PAB01"
     *  ["status"]=> string(1) "1"
     *  ["date"]=> string(14) "20160527023609"
     *  ["accNo"]=> string(4) "0018"
     *  ["telephone"]=> string(11) "135****5506"
     *  ["customerId"]=> string(4) "0001"
     *  ["OpenId"]=> string(26) "unionpay012016052743419513"
     *  ["orderId"]=> string(26) "unionpay012016052743419513"
     *  ["bankType"]=> string(2) "01"
     *  ["plantBankName"]=> string(12) "平安银行" }
     *
     */

    /**
     * 获取签名过后的原始数据orig和签名数据sign
     * @param file $merchantCertFile
     * @param string $data
     * @return list
     */
    private function _getOrigAndSing($merchantCertFile, $data) {
        // require_once '../lib/pingan.php';
        $pingan = new pingan();

        $orig = $pingan->getOrig($data);
        $sign = $pingan->getSign($merchantCertFile, $data);

        return array($orig, $sign);
    }

    /**
     * 将数据组装为xml格式的数据
     * @param array $data
     * @return string
     *
     * $data = '<kColl id="input" append="false"><field id="masterId" value="2000311146"/><field id="orderId" value="20003111462015060473550416"/><field id="currency" value="RMB"/><field id="amount" value="0.01"/><field id="objectName" value="??"/><field id="paydate" value="20150604143506"/><field id="remark" value="??"/><field id="validtime" value="0"/></kColl>';
     */
    private function _getData($data) {
        $xml_data = '<kColl id="input" append="false">';
        foreach ($data as $key => $value) {
            $xml_data .= '<field id="' . $key . '" value="' . $value . '"/>';
        }
        $xml_data .= '</kColl>';

        return $xml_data;
    }

}

/*$payClass = new pay();
$payClass->run();*/